package com.woke.addressactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.http.HttpMethods;
import com.woke.data.ExchangeType;
import com.woke.fragment.ExchangeFragment;
import com.zhongcai.online.R;
import com.wokeMy.view.LotteryMainActivity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * z中交所
 */
public class ExChangeActivity extends AppCompatActivity implements OnPageChangeListener, TabLayout.OnTabSelectedListener {

    private ViewPager mViewPager;
    private ChangePromoteAdapter mPromoteAdapter;
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private TextView mTitleTv;
    private int mSort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleTv = (TextView) findViewById(R.id.toolbar_title);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.addOnTabSelectedListener(this);
        mSort = getIntent().getIntExtra("sort",-1);
        HttpMethods.getInstance().getExchangeType(mExChangeTypeListObserver);
    }

    private Observer<List<ExchangeType>> mExChangeTypeListObserver = new Observer<List<ExchangeType>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(List<ExchangeType> value) {
            if (value != null && value.size() > 0) {
                for (int i = 0; i < value.size(); i++) {
                    ExchangeType exchangeType = value.get(i);
//                    if (i == 0) {
//                        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView(exchangeType.getTitle())));
//                    } else if (i == value.size() - 1) {
//                        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView(exchangeType.getTitle())));
//                    } else {
//                        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView(exchangeType.getTitle())));
//                    }
                    mTabLayout.addTab(mTabLayout.newTab().setText(exchangeType.getTitle()));
                }
//                initToolbarBg();

                mPromoteAdapter = new ChangePromoteAdapter(getSupportFragmentManager(), value);
                mViewPager.setAdapter(mPromoteAdapter);
                mViewPager.setOnPageChangeListener(ExChangeActivity.this);
                if(mSort==-1){
                    if (value.size() > 1) {
                        mViewPager.setCurrentItem(1);
                    } else {
                        mViewPager.setCurrentItem(0);

                    }
                }else{
                    for (int i=0;i<value.size();i++){
                        ExchangeType type=value.get(i);
                        if(type.getSort()==mSort){
                            mViewPager.setCurrentItem(i);
                            break;
                        }
                    }
                }
            }
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    };

	/*----------------------------------------------------------------------*/

    private class ChangePromoteAdapter extends FragmentPagerAdapter {

        private List<ExchangeType> mExchangeTypes;

        public ChangePromoteAdapter(FragmentManager fm, List<ExchangeType> value) {
            super(fm);
            mExchangeTypes = value;
        }


        @Override
        public Fragment getItem(int arg0) {
            ExchangeFragment fragment = new ExchangeFragment();
            Bundle args = new Bundle();
            args.putInt("type", mExchangeTypes.get(arg0).getSort());
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mExchangeTypes == null ? 0 : mExchangeTypes.size();
        }

    }


    private int mPreviousScrollState;
    private int mScrollState;

    @Override
    public void onPageScrollStateChanged(int state) {
        mPreviousScrollState = mScrollState;
        mScrollState = state;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mTabLayout != null) {
            final boolean updateText = (mScrollState == ViewPager.SCROLL_STATE_DRAGGING)
                    || (mScrollState == ViewPager.SCROLL_STATE_SETTLING
                    && mPreviousScrollState == ViewPager.SCROLL_STATE_DRAGGING
            );
            mTabLayout.setScrollPosition(position, positionOffset, updateText);
        }

    }

    @Override
    public void onPageSelected(int position) {
        try {
            if (position==3){
                Intent intent =new Intent(ExChangeActivity.this, LotteryMainActivity.class);
                startActivity(intent);
            }else {
                mTabLayout.getTabAt(position).select();
            }
        } catch (Exception e) {
        }
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        mViewPager.setCurrentItem(tab.getPosition(), false);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }


    private View getTabItemCustonView(String title) {
        View rootView = LayoutInflater.from(this).inflate(R.layout.tablayout_tv, null);
        TextView tv = (TextView) rootView.findViewById(R.id.tab_tv);
        tv.setText(title);
        return rootView;
    }

//    private void initToolbarBg() {
//        if (mTabLayout != null) {
//            int count = mTabLayout.getTabCount();
//            try {
//                if (count > 1) {
//                    for (int i = 0; i < count; i++) {
//                        View tabView = (View) mTabLayout.getTabAt(i).getCustomView().getParent();
//                        if (i == 0) {
//                            //第一个
//                            tabView.setBackgroundResource(R.drawable.zjs_tab_left_selector);
//                        } else if (i == count - 1) {
//                            //最后一个
//                            tabView.setBackgroundResource(R.drawable.zjs_tab_right_selector);
//                        } else {
//                            //中间选项
//                            tabView.setBackgroundResource(R.drawable.zjs_tab_middle_selector);
//                        }
//                    }
//                } else if (count == 1) {
//                    View tabView = (View) mTabLayout.getTabAt(0).getCustomView().getParent();
//                    tabView.setBackgroundResource(R.drawable.zjs_tab_whole_selector);
//                }
//            } catch (Exception e) {
//            }
//
//        }
//    }

}
